"""
Case Type   : 导入导出、备份恢复工具支持M*兼容性
Case Name   : gs_dump导出导入前后测试show function status语法
Create At   : 2023.05.04
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.兼容b库创建自定义函数
    3.使用gs_dump工具导出表为tar格式
    4.导入之前导出的数据到新库
    5.连接导入库查询自定义函数
    6.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
History     :modified by z@wan005，2023.08.18，修改step4
             modified by zhanghuan96 at 2025/2/27，优化用例
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GsDump0016(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.dbuser_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.db_name = "db_dump_0016"
        self.db_name_01 = "db_dump_0016_01"
        self.function_name = "func_dump_0016"
        self.dump_path = os.path.join(macro.DB_BACKUP_PATH, 'dump0016')

    def test_server_tools(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '----step1: 创建兼容b库 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} encoding 'UTF8' " \
                  f"dbcompatibility 'b';" \
                  f"drop database if exists {self.db_name_01};" \
                  f"create database {self.db_name_01} encoding 'UTF8' " \
                  f"dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_result)
        self.assertEqual(2, sql_result.count(
            self.constant.CREATE_DATABASE_SUCCESS), '执行失败:' + text)

        text = '---step2: 兼容b库创建自定义函数 expect: 成功---'
        self.log.info(text)
        sql_cmd = f'''drop function if exists {self.function_name};
        create function {self.function_name}(integer, integer)returns \
        integer as 'select \$1 +\$2;' language sql immutable returns null \
        on null input;
        show function status like '%{self.function_name}';'''
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, sql_result,
                      '执行失败:' + text)
        self.assertIn('| func_dump_0016 | FUNCTION |', sql_result,
                      '执行失败:' + text)

        text = '----step3: 使用gs_dump工具导出表为tar格式 expect: 成功----'
        self.log.info(text)
        gs_dump_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"gs_dump " \
                      f"-p {self.dbuser_node.db_port} " \
                      f"{self.db_name} -F t " \
                      f"-f {self.dump_path}"
        self.log.info(gs_dump_cmd)
        dump_msg = self.dbuser_node.sh(gs_dump_cmd).result()
        self.log.info(dump_msg)
        self.assertIn(f"dump database {self.db_name} successfully",
                      dump_msg, '执行失败:' + text)

        text = '----step4: 导入之前导出的数据到新库 expect: 成功----'
        self.log.info(text)
        gsql_cmd = f"source {macro.DB_ENV_PATH};" \
                   f"gs_restore -p {self.dbuser_node.db_port} " \
                   f"-d {self.db_name_01} " \
                   f"{self.dump_path}"
        self.log.info(gsql_cmd)
        restore_msg = self.dbuser_node.sh(gsql_cmd).result()
        self.log.info(restore_msg)
        self.assertIn(self.constant.GSQL_RESTORE_SUCCESS_MSG, restore_msg,
                      '执行失败:' + text)

        text = '-step5: 连接导入库查询自定义函数 expect: 成功--'
        self.log.info(text)
        sql_cmd = f"show function status like '%{self.function_name}';"
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname=self.db_name_01)
        self.log.info(sql_result)
        self.assertIn('| func_dump_0016 | FUNCTION |', sql_result,
                      '执行失败:' + text)

    def tearDown(self):
        text = '---step6: 清理环境 expect: 成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"drop database if exists {self.db_name_01};"
        self.log.info(sql_cmd)
        sql_result = self.sh_primary.execut_db_sql(sql_cmd,
                                                   dbname='postgres')
        self.log.info(sql_result)
        rm_cmd = f"rm -rf {self.dump_path}"
        self.log.info(rm_cmd)
        rm_msg = self.dbuser_node.sh(rm_cmd).result()
        self.log.info(rm_msg)
        self.assertEqual(2,
                         sql_result.count(self.constant.DROP_DATABASE_SUCCESS))
        self.assertEqual('', rm_msg)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
